package com.example.poemtest.index.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.poemtest.R;
import com.example.poemtest.category.PoetryDynastyActivity;
import com.example.poemtest.entity.PoetryInfoBean;
import com.example.poemtest.index.adapter.SearchAdapter;
import com.example.poemtest.utils.Constant;
import com.example.poemtest.utils.FullScreenUtil;

import org.apache.commons.lang3.StringUtils;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class SearchResult extends AppCompatActivity {

    private EditText edit;   //搜索编辑框
    private Button btn;  //搜索按钮
    private ListView listView;
    private ImageView delete;   //删除搜索框的内容
    private List<String> list = new ArrayList<>();  //设置数据源
    private String search = "";
    private Button back1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FullScreenUtil.getInstance().fullScreen(this,true);
        setContentView(R.layout.activity_search_result);

        Intent intent = getIntent();
        search = intent.getStringExtra("search");
        Log.e("search",search.toString());
        //设置控件对象
        setView();

        back1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent intent = new Intent(SearchResult.this, );
//                startActivity(intent);
                finish();
            }
        });

        /**
         * EditText添加监听
         */
        edit.addTextChangedListener(new TextWatcher() {
            //文本改变之前执行
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            //文本改变的时候执行
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //如果长度为0
                if (s.length() == 0) {
                    //隐藏“删除”图片
                    delete.setVisibility(View.GONE);
                } else {//长度不为0
                    //显示“删除图片”
                    delete.setVisibility(View.VISIBLE);
                }
            }
            //文本改变之后执行
            public void afterTextChanged(Editable s) { }
        });

        //设置搜索框不可编辑只可点击
        setCanNotEditAndClick();
        //设置数据源
        list = setData();
        Log.e("list", list.toString());
//        list = setData();
        //设置适配器
        SearchAdapter searchAdapter = new SearchAdapter(this, R.layout.item_search_result_poetry, list);
        /*FriendListViewAdapter listViewAdapter = new FriendListViewAdapter(
                this,
                R.layout.item_friend,
                list
        );*/
        Log.e("数据源", Constant.searchList.toString());
        //绑定适配器
        listView.setAdapter(searchAdapter);
        //点击每个小的item
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.e("选中的诗词",list.get(position));
                for (PoetryInfoBean.PoetryInfo poetryInfo : Constant.poetryInfos) {
                    if (StringUtils.equals(poetryInfo.getPoetryContent(),list.get(position))) {
                        Intent intent1 = new Intent(SearchResult.this, StudyPoetryDetailActivity.class);
                        intent1.putExtra("poetry",poetryInfo);
                        startActivity(intent1);
                    }
                }
            }
        });

    }

    //设置数据源
    public List<String> setData() {
        List<String> stringList = new ArrayList<>();
//        InputStream input = getResources().openRawResource((R.raw.shi300));
        try {
            int k = 0;
//            String temp = "";
//            String result = "";
//            Scanner scan = new Scanner(input);
//            int max=100;
//            while (scan.hasNext()) {
//                String str = scan.nextLine();
//                if (str.contains("：")) {
//                    temp = str;
//                    continue;
//                }
//                if (str.contains(search)) {
//                    k++;
//                    result = k + "." + str + temp;
//                    stringList.add(result);
//                    max--;
//                    if (max<=0) break;
//                }
//            }
            for (PoetryInfoBean.PoetryInfo poetryInfo : Constant.poetryInfos) {
//                        Log.e("能不能",poetryInfo.getPoetryContent());
//                        if (poetryInfo.getPoetryContent().contains(chooseText)){
//                            queryDataList.add(poetryInfo);
//                        }
                if (StringUtils.contains(poetryInfo.getPoetryContent(), search)) {
                    stringList.add(poetryInfo.getPoetryContent());
                    k++;
                }
            }

            Toast.makeText(SearchResult.this, "一共有" + k + "句！", Toast.LENGTH_SHORT).show();
//            res.setText(result);
//            res.setMovementMethod(ScrollingMovementMethod.getInstance());
//            scan.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stringList;
    }

    /**
     * 设置控件对象
     */
    private void setView() {
        edit = findViewById(R.id.edit);
        edit.setText(search);
        btn = findViewById(R.id.btn_search);
        listView = findViewById(R.id.list_view);
        delete = findViewById(R.id.delete);
        back1 = findViewById(R.id.to_search_back);
    }

    /**
     * 设置搜索框不可编辑只可点击
     */
    private void setCanNotEditAndClick() {
        edit.setFocusable(false);
        edit.setFocusableInTouchMode(false);
        edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到搜索页面
                Intent intent = new Intent(SearchResult.this, SearchActivity.class);
                startActivityForResult(intent,10086);
            }
        });
    }

}